/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package battle.ship.model;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Carlos
 */
public class Board {
    private final int size; 
    private List<Ship> shipList;
    
    /**
     *
     */
    public Board() {
        this.size = 10;
        this.shipList = new ArrayList(11);
    }

    /**
     *
     * @return
     */
    public int getSize() {
        return size;
    }

    /**
     *
     * @return
     */
    public List<Ship> getShips() {
        return shipList;
    }
    
    /**
     *
     * @param ship
     * @return 
     */
    public boolean addShip(Ship ship){
        boolean validationResult = validateShip(ship);
        if (validationResult){
            this.shipList.add(ship);
        }
        return validationResult;
    }
    
    /**
     *
     * @param ship
     * @return
     */
    public boolean validateShip(Ship ship){
        for (Point point : ship.getPoints()){
            if (!validatePoint(point)){
                return false;
            }
        }
        return true;
    }
    
    /**
     *
     * @param point
     * @return
     */
    public boolean validatePoint(Point point){
        if (point.getX() < this.size && point.getY() < this.size){
            if (this.shipList == null){
                return true;
            }
            for (Ship ship : this.shipList){
                for (Point shipPoint : ship.getPoints()){
                    if (point.equals(shipPoint)){
                        return false;
                    }
                }
            }
            return true;
        }
        return false;
    }
    
    
    
    /**
     *
     * @param shipoint
     * @param ship
     * @return
     */
    public boolean createShip(Point shipoint, Ship ship){
        /*
         * cria coordenadas a partir o coordenada inicial para o resto das posiçoes do barco
         */
        ship.addPoint(shipoint);
        
        boolean result = validateShip(ship);
        
        if(result)
        {
          this.addShip(ship);
        }
        
        return result;
    } 
    
}
